package managers;

import java.util.ArrayList;

import finobjects.Bin;


public class BinManager {
	private static ArrayList<Bin> bins;
	private static boolean initialized = false; 
	
	
	public static void addBin (Bin b) {
		if(!BinManager.initAndCheckIfContains(b)) {
			BinManager.bins.add(b);
		}
		else {
			System.out.println("WARN: Bin already in binslist!");
		}
	}
	
	public static ArrayList<Bin> getBins() {
		return BinManager.bins;
	}
	
	public static void removeBin (Bin b) {
		if(BinManager.initAndCheckIfContains(b)) {
			BinManager.bins.remove(b);
		}
		else {
			System.out.println("WARN: Bin not in binslist!");
		}
	}
	
	public static Bin getBinByNumber(int number){
		initBinManager();
		for(Bin b : BinManager.bins){
			if(b.getBinNumber() == number){
				return b;
			}
		}
		return null;
	}
	
	private static void initBinManager() {
		if(!BinManager.initialized) {
			BinManager.bins = new ArrayList<Bin>();
			BinManager.initialized= true; 
		}
	}
	
	private static boolean initAndCheckIfContains(Bin b) {
		boolean out = false;
		initBinManager();
			
		if(BinManager.bins.contains(b)) {
			out = true;
		}
		
		return out;
	}
	
	public static int generateBinNumber() {
		int out = 0;
		boolean newNumberFound = false;
		
		while(!newNumberFound) {
			if(getBinByNumber(out) == null) {
				newNumberFound = true;
			}
			out++;
		}
		return out;
	}
			
		
}